﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace CSharpBot
{
    public static class QuoteBot
    {
        static readonly string botName = "QuoteBot";

        public static void init()
        {
            Console.Write("[");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(botName);
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("] Please enter a command!");
            Console.WriteLine();

            Console.Title = "C# " + botName + " is up and running!";

        quoteRestart:
            Console.Write("Command: ");
            Console.ForegroundColor = ConsoleColor.Magenta;
            string command = Console.ReadLine();
            Console.ForegroundColor = ConsoleColor.Gray;

            string[] arguments = command.Split(' ');

            if (arguments.Length > 0)
            {
                if (arguments[0] == "clear")
                    _do(1);
                if (arguments[0] == "help")
                    _do(0);
                if (arguments[0] == "exit")
                    Environment.Exit(0);
                if (arguments[0] == ".quote")
                    if (arguments.Length > 1)
                    {
                        if (arguments[1] == "add")
                            if (arguments.Length > 2)
                                addQuote(arguments[2]);
                            else
                                Console.WriteLine("[" + botName + "] Not enough arguments");

                        if (arguments[1] == "del")
                            if (arguments.Length > 2)
                                delQuote(arguments[2]);

                        if (arguments[1] == "read")
                            if (arguments.Length > 2)
                                reaQuote(arguments[2]);
                    }
                    else
                        Console.WriteLine("[" + botName + "] Not enough arguments");
            }
            else
                Console.WriteLine("[" + botName + "] Not enough arguments");

            goto quoteRestart;
        }

        private static void _do(int what)
        {
            if (what == 0) //Shows the help menu
            {
                Console.Write("[");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(botName);
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("] ");
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write("clear - Clears the screen\r\n");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("[");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(botName);
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("] ");
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write("help - Shows this\r\n");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("[");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(botName);
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("] ");
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write(".quote add QUOTE - Adds a quote\r\n");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("[");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(botName);
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("] ");
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write(".quote del QUOTE_NUM - Deletes QUOTE_NUM from Quote\r\n");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("[");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(botName);
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("] ");
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write(".quote read QUOTE_NUM - Reads a quote from a specific number");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine();
            }
            if (what == 1) //Clears the screen
            {
                Console.Clear();
                Console.Write("[");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(botName);
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("] Please enter a command!");
                Console.WriteLine();
            }
        }
        static void delQuote(string qN0)
        {
            if (!File.Exists(@Environment.CurrentDirectory + @"\Quotes\" + qN0 + ".ini"))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("[");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(botName);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("] Quote #" + qN0 + " does not exist!");
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Gray;
            }
            else
            {
                File.Delete(@Environment.CurrentDirectory + @"\Quotes\" + qN0 + ".ini");
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("[");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(botName);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("] Quote #" + qN0 + " has been deleted!");
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Gray;
            }
        }
        static void addQuote(string quote)
        {
            INIFile ini1 = new INIFile(@Environment.CurrentDirectory + @"\Settings.ini");
            INIFile ini2 = new INIFile(@Environment.CurrentDirectory + @"\Quotes\" + ini1.Read(botName, "Quotes") + ".ini");

            string qN1 = ini1.Read(botName, "Quotes");
            int qN2 = Convert.ToInt32(qN1) + 1;
            string qN3 = Convert.ToString(qN2);

            ini2.Write(botName, "quote", quote);
            ini1.Write(botName, "Quotes", qN3);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("[");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(botName);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("] Quote #" + qN1 + " has been added!");
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Gray;
        }
        static void reaQuote(string qN0)
        {
            if (!File.Exists(@Environment.CurrentDirectory + @"\Quotes\" + qN0 + ".ini"))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("[");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(botName);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("] Quote #" + qN0 + " does not exist!");
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Gray;
            }
            else
            {
                INIFile ini = new INIFile(@Environment.CurrentDirectory + @"\Quotes\" + qN0 + ".ini");
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("[");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(botName);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("] Quote #" + qN0 + ": ");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(ini.Read(botName, "quote"));
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Gray;
            }
        }
    }
}
